﻿package com.eitb.programas.streamingTest{
	import flash.geom.*;
	import flash.display.*;
	import flash.text.*;
	
	import flash.events.*;
	
	import com.eitb.programas.*;
	import com.eitb.literales.*;
	import com.eitb.botones.HorizontalScrollBar;
	import com.eitb.botones.VerticalScrollBar;
	import com.eitb.programas.streamingTest.botones.*;
	import com.eitb.programas.streamingTest.literales.*;
	import com.eitb.programas.streamingTest.properties.*;
	import com.eitb.properties.*;
	import com.eitb.imagenes.*;
	import com.eitb.bitmaps.*;
	import com.eitb.cargadores.*;
	import com.eitb.text.*;
	import com.eitb.reproductores.*;
	import com.oneko.publicidad.PubliMask;
	import com.eitb.geom.*;

	public class Main extends Sprite {
		private var formato:String=loaderInfo.parameters.formato;
		private var hostname:String=loaderInfo.parameters.hostname;
		private var filename:String=loaderInfo.parameters.filename;
		private var idioma:String=loaderInfo.parameters.idioma;
		private var _scCG:String=loaderInfo.parameters.nombre; // sustituir por tipo ¿y crear uno nuevo que sea titulo?
		private var portada:String=loaderInfo.parameters.portada;
		private var autoplay:String=loaderInfo.parameters.autoplay;
		private var publiURL:String=loaderInfo.parameters.publiURL;

//		private var hostname:String = "flashvod.eitb.com/ondemand";
//		private var filename:String = "multimedia/videos/2009/10/09/183219/OBAMA_OFF_20091009_115305";
//		private var formato:String = StagePropertiesFactory.UNA_COLUMNA_CUATROTERCIOS;
//		private var idioma:String = "es";
//		private var _scCG:String = "test";
//		private var autoplay:String = "true";
//		private var portada:String = "/media/images/2010/01/08/226433/226433_fernando_cuesta_elurra_gasteiz_tranbia_dest_2.jpg";
////		private var publicidad:String="http://ads.eitb.com/2/es.eitb.com/team/1623499044@x90";
//		private var publiURL:String="http://ads.eitb.com/2/es.eitb.com/tv/1623499044@x90";


//		private var hostname:String = "cp70268.live.edgefcs.net/live";
//		private var filename:String = "EITB_especiales_geo@11956";
//		private var filename:String = "eitb-CanalVasco@5519";
//		private var filename:String = "eitb-ETBSat@5219";


//		private var hostname:String = "cp70268.edgefcs.net/ondemand";
//		private var hostname:String = "cp72840.edgefcs.net/ondemand";

//		private var filename:String = "multimedia/videos/2010/02/18/245644/conquis_aventura_ona_20100217_170304";
//		private var filename:String = "multimedia/videos/2009/10/14/185007/LaHuerfana_20091014_091303";

		private static var vStreaming:IVistaStreaming;
		private var properties:Object;

		public function Main():void {
			this.initStreaming();
		}
		
		private function initStreaming():void {
			var vSize:Object;
			var idiomaId:int = Literales.getIdiomaId(this.idioma);
			var formatoTexto:TextFormat=new TextFormat();
			var mStreaming:ModeloStreaming= new ModeloStreaming();
			var dStreaming:DialogoStreaming = new DialogoStreaming();
			addChild(dStreaming);
			Main.vStreaming = dStreaming;
			
			this.properties = StagePropertiesFactory.getFormato(this.formato);
			vSize = {width:this.properties.VIDEO.width, height:this.properties.VIDEO.height};
			
			dStreaming.initControladorStreaming(mStreaming);
			mStreaming.initVistaStreaming(dStreaming);
			
			mStreaming.initIdioma(idiomaId);
			mStreaming.initLiterales(new StreamingLiterales(idiomaId));
			mStreaming.initLiteralesCargador(new CargadorLiterales(idiomaId));
			mStreaming.initLiteralesControles(new ControlesLiterales(idiomaId));

	
			mStreaming.initFormato(this.properties);
			mStreaming.initHostname(this.hostname);
			mStreaming.initFilename(this.filename);
//			mStreaming.initCanal(this._scCG);
//			mStreaming.initNielsen(this._scCG);
			mStreaming.initAutoplay(this.autoplay);
//			mStreaming.initPublicidad(this.publicidad);
			dStreaming.initStage(this.properties.STAGE);
			
			// PREPARA VIDEO
			mStreaming.initVistaVideo(getVideo(this.properties));
			
			// PREPARA PORTADA
			mStreaming.initVistaPortada(getPortada(this.properties), this.portada);
			
			// PREPARA CONTENEDOR
			mStreaming.initVistaControles(getControles(this.properties));
			
			// PREPARA PANEL DE CONTROL DE EVENTOS DEL MOUSE
			mStreaming.initVistaZonaControl(getZonaControl(this.properties));

			// PREPARA FONDO DE CONTROLES
			mStreaming.initVistaBackground(getBackground(this.properties));
			
			// PREPARA BARRA DE TIEMPO
			mStreaming.initVistaTimeline(getTimeline(this.properties));
			
			// PREPARA BARRA DE VOLUMEN
			mStreaming.initVistaVolumen(getVolumen(this.properties));

			// PREPARA BOTON DE MAXIMIZAR
			mStreaming.initVistaMaximize(getMaximize(this.properties));
			
			// PREPARA BOTON DE PAUSE
			mStreaming.initVistaPause(getPause(this.properties));
			
			// PREPARA BOTON DE STOP
			mStreaming.initVistaStop(getStop(this.properties));
			
			
			// PREPARA CARGADOR			
			mStreaming.initVistaCargador(getCargador(this.properties));
			
			// PREPARA BOTON DE PLAY
			mStreaming.initVistaPlay(getPlay(this.properties));
			
			// Prepara publicidad
			mStreaming.initVistaPubli(getPubli(this.properties));
			
			// ACCIONES
			dStreaming.fitScreen();
			mStreaming.initAkamaiConnection();
		}

		private function getVideo(properties:Object):SimpleVideo {
			var video:SimpleVideo = new SimpleVideo();
			video.setSize(properties.VIDEO.width,properties.VIDEO.height);
			video.x=properties.VIDEO.x;
			video.y=properties.VIDEO.y;
			//video.video.deblocking=properties.VIDEO.deblocking;
			//video.video.smoothing=properties.VIDEO.smoothing;
			
			return video;
		}
		
		private function getPortada(properties:Object):Grafico {
			var portada:Grafico = new Grafico();
			return portada;
		}
		private function getControles(properties:Object):Grafico {
			return new Grafico();
		}


		private function getZonaControl(properties:Object):Grafico {
			var centrado:Point;
			var zonaControl:Grafico = new Grafico();
			var shape:Shape = new Shape();
			zonaControl.addChild(shape);

			shape.graphics.beginFill(properties.BACKGROUND.color);
			shape.graphics.drawRoundRect(0,0,properties.BACKGROUND.width, properties.BACKGROUND.height, 5, 5);
			shape.graphics.endFill();

			zonaControl.alpha=0;

			return zonaControl;
		}
		
		private function getBackground(properties:Object):Grafico {
			var centrado:Point;
			var background:Grafico = new Grafico();
			var shape:Shape = new Shape();
			background.addChild(shape);
			
			shape.graphics.beginFill(properties.BACKGROUND.color);
			shape.graphics.drawRoundRect(0,0,properties.BACKGROUND.width, properties.BACKGROUND.height, 5, 5);
			shape.graphics.endFill();
			
			background.alpha=properties.BACKGROUND.alpha;
			background.cacheAsBitmap = true;
			
			return background;
		}
		
		private function getPlay(properties:Object):BotonPlay {
			var play:BotonPlay = new BotonPlay();
			play.alpha=properties.PLAY_BUTTON.alpha;
			
			play.x=properties.PLAY_BUTTON.x - play.width/2;
			play.y=properties.PLAY_BUTTON.y - play.height/2;
			play.visible=false;
			
			return play;
		}
		
		private function getPause(properties:Object):BotonPause {
			var pause:BotonPause = new BotonPause();
			pause.alpha=properties.PAUSE_BUTTON.alpha;
			
			pause.x=properties.PAUSE_BUTTON.x - pause.width/2;
			pause.y=properties.PAUSE_BUTTON.y - pause.height/2;
			pause.visible=false;
			
			return pause;
		}
		
		private function getStop(properties:Object):BotonStop {
			var stop:BotonStop = new BotonStop();
			stop.alpha=properties.STOP_BUTTON.alpha;
			
			stop.x=properties.STOP_BUTTON.x-stop.width/2;
			stop.y=properties.STOP_BUTTON.y-stop.height/2;
			stop.visible=false;
			return stop;			
		}
		
		private function getMaximize(properties:Object):BotonMaximize {
			var maximize:BotonMaximize = new BotonMaximize();
			maximize.alpha=properties.MAXIMIZE_BUTTON.alpha;
			
			maximize.x=properties.MAXIMIZE_BUTTON.x-maximize.width/2;
			maximize.y=properties.MAXIMIZE_BUTTON.y-maximize.height/2;
			maximize.visible=false;
			return maximize;			
		}
		
		private function getCargador(properties:Object):CargadorEitb {
			var cargador:CargadorEitb;
			cargador = new CargadorEitb();
			cargador.alpha=properties.CARGADOR.alpha;
			cargador.x=properties.CARGADOR.x-cargador.width/2;
			cargador.y=properties.CARGADOR.y-cargador.height/2;
			cargador.visible=false;
			return cargador;
		}
		
		private function getVolumen(properties:Object):VolumeBar {
			var background:Shape;
			var bar:Shape;
			var icono:Bitmap;

			var volume:VolumeBar = new VolumeBar();
			background = new Shape();
			bar = new Shape();

			background.graphics.beginFill(properties.VOLUME.BACKGROUND.color);
			background.graphics.drawRoundRect(0,0,properties.VOLUME.BACKGROUND.width, properties.VOLUME.BACKGROUND.height, 5, 5);
			background.graphics.endFill();
			background.alpha=properties.VOLUME.BACKGROUND.alpha;

			bar.graphics.beginFill(properties.VOLUME.POINTER.color);
			bar.graphics.drawRoundRect(0,0,properties.VOLUME.POINTER.width, properties.VOLUME.POINTER.height, 5, 5);
			bar.graphics.endFill();
			bar.alpha=properties.VOLUME.POINTER.alpha;
			
			icono = new Bitmap(new VolumeOn());
			icono.width = icono.height * (background.height / icono.height)
			icono.height = background.height;
			icono.x = background.width - icono.width;
			
			volume.x=properties.VOLUME.x;
			volume.y=properties.VOLUME.y;

			volume.initPointer(bar);
			volume.initBackground(background);
			volume.initText(properties.VOLUME.TEXT);
			volume.initIcono(icono);

			return volume;
		}
		
		private function getTimeline(properties:Object):TimelineBar {
			var background:Shape;
			var bar:Shape;

			var timeline:TimelineBar = new TimelineBar();
			background = new Shape();
			bar = new Shape();

			background.graphics.beginFill(properties.TIMELINE.BACKGROUND.color);
			background.graphics.drawRoundRect(0,0,properties.TIMELINE.BACKGROUND.width, properties.TIMELINE.BACKGROUND.height, 5, 5);
			background.graphics.endFill();
			background.alpha=properties.TIMELINE.BACKGROUND.alpha;

			bar.graphics.beginFill(properties.TIMELINE.POINTER.color);
			bar.graphics.drawRoundRect(0,0,properties.TIMELINE.BACKGROUND.width, properties.TIMELINE.BACKGROUND.height, 5, 5);
			bar.graphics.endFill();
			bar.alpha=properties.TIMELINE.POINTER.alpha;

			timeline.x=properties.TIMELINE.x;
			timeline.y=properties.TIMELINE.y;

			timeline.initPointer(bar);
			timeline.initBackground(background);
			timeline.initText(properties.TIMELINE.TEXT);
//			timeline.initIcono(new Bitmap(new VolumeOn()));
			
			return timeline
		}		
		
		private function getPubli(properties:Object):PubliMask{
			if (publiURL!=""){
				properties.PUBLI.url=publiURL;
			}
			var publi:PubliMask=new PubliMask(properties);
			return publi;
		}
	}
}